package sma.fbss.model;

import sma.fbss.database.Database;
//import ifi.sma.mss.utils.Accessary;

import java.util.ArrayList;
//port java.util.Date;
import java.util.Map;

public class MDFBaller {
	public String id_footballer = "";
	public String name_footballer;
	public long birthday;
	public float height;
	public float salary;
	public float price;
	public boolean libre;
	public String description;
	public String club;
	
	public String AID;

	public MDFBaller() {
	}

	public MDFBaller(String name) {
		if (name.equals(""))
			return;
		try {
			AID = name;
			name = name.substring(0, name.indexOf("@"));
			Database database = new Database("FBMarket");
			String str_sql = "select * from footballer where id_footballer = " + name;
			ArrayList<Map<String, String>> datas = database.get(str_sql);
			if (datas.size() > 0) {
				this.id_footballer = datas.get(0).get("id_footballer").toString();
				this.name_footballer = datas.get(0).get("name_footballer");
				this.height = Float.valueOf(datas.get(0).get("height"));
				this.birthday = Long.valueOf(datas.get(0).get("birthday"));
				this.salary = Float.valueOf(datas.get(0).get("salary"));
				this.price = Float.valueOf(datas.get(0).get("price"));
				this.libre = Boolean.valueOf(datas.get(0).get("libre"));
				this.description = datas.get(0).get("description");
				this.club = datas.get(0).get("club");
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public boolean update() {
		try {
			String str_sql = "";
			Database database = new Database("FBMarket");
			if (this.id_footballer == "") {

				str_sql = "INSERT INTO footballer(id_footballer, name_footballer, height, " + 
				"birthday, salary, price, libre, description, club) VALUES('" + 
				this.id_footballer + "','" + this.name_footballer + "','" + 
				this.height + this.birthday + "','" + this.salary + "','" +
				this.price + "','" + this.libre + "','" + this.description + "','" +
				this.club + "','" +	"')";
				database.set(str_sql);
				return true;
			} else {
				str_sql = "UPDATE footballer set name_footballer = '" + this.name_footballer + 
				"', height = '" + this.height + "', birthday = '" + this.birthday +  
				"', salary = '" + this.salary + "', price = '" + this.price + 
				"', libre = '" + this.libre + "', description = '" + this.description + 
				"', club = '" + this.club + "' WHERE id_footballer = " + this.id_footballer;
				database.set(str_sql);
				return true;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}

	}
	
	public static MDFBaller[] getFBallerwithClub(String id_club) throws Exception
	{
		Database database = new Database("FBMarket");
		String str_sql = "select * from footballer";
		if(!id_club.equals(""))
			str_sql = "select * from footballer where club = '" + id_club + "'";
		ArrayList<Map<String, String>> datas = database.get(str_sql);
		if(datas != null)
		{
			MDFBaller[] fb_list = new MDFBaller[datas.size()];
			for(int i=0;i<datas.size();i++)
			{
				try{
					fb_list[i] = new MDFBaller();
					fb_list[i].id_footballer = datas.get(i).get("id_footballer").toString();
					fb_list[i].name_footballer = datas.get(i).get("name_footballer");
					fb_list[i].birthday = Long.parseLong(datas.get(i).get("birthday"));
					fb_list[i].height = Float.parseFloat(datas.get(i).get("height"));
					fb_list[i].price = Float.parseFloat(datas.get(i).get("price"));
					fb_list[i].salary = Float.parseFloat(datas.get(i).get("salary"));
					fb_list[i].description = datas.get(i).get("description");
					fb_list[i].club = datas.get(i).get("club").toString();
					fb_list[i].libre = Boolean.parseBoolean(datas.get(i).get("libre"));
				} catch(Exception ex)
				{
					ex.printStackTrace();
				}
			}
			return fb_list;
			
		}
		return null;
	}
}

